<template>
  <div class="app-container">
    <u-page title="互斥管理">
      <div slot="header">
        <el-button type="primary" icon="el-icon-plus" @click="hdlOpen(newForm)">
          {{ textMap['create'] }}
        </el-button>
      </div>

      <u-filtered>
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="档期主题" prop="scheduleId">
            <Schedule type="coupon" v-model="listQuery.schedule" width="160px" @change="changeScheduleQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button type="text" @click="hdlEdit(row)">编辑</el-button>
              <el-button type="text" @click="hdlDelete(row.id, '记录')">删除</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>
    </u-page>
    <u-dialog :title="textMap[dialogStatus]" :show.sync="dialogFormVisible" width="600px" @confirm="dialogStatus==='create'? hdlCreate():hdlUpdate()">
      <el-form ref="dialogForm" :model="dataForm" :rules="rules" label-width="100px">
        <el-form-item label="互斥类型" prop="type" >
          <el-radio-group v-model="dataForm.type">
            <el-radio-button :label="1">小票互斥</el-radio-button>
            <el-radio-button :label="2">活动互斥</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="档期主题A" prop="scheduleA">
          <Schedule type="coupon" valid="valid" v-model="dataForm.scheduleA" style="width: 100%" @change="changeScheduleA"/>
        </el-form-item>
        <el-form-item label="档期主题B" prop="scheduleB">
          <Schedule type="coupon" valid="valid" v-model="dataForm.scheduleB" style="width: 100%" @change="changeScheduleB"/>
        </el-form-item>
      </el-form>
    </u-dialog>
  </div>
</template>

<script>
import Schedule from '../components/schedule'
export default {
  components: {
    Schedule
  },
  data() {
    return {
      api: this.$u.api.RebatenewMutex,
      dataForm: {
        scheduleA: {},
        scheduleB: {},
        scheduleAId: '',
        scheduleBId: ''
      },
      newForm: {
        type: 1
      },
      rules: {
        validityDate: [
          { required: true, message: '时间必选', trigger: 'blur' }
        ],
        cardTypeList: [
          { required: true, message: '活动对象必选', trigger: 'blur' }
        ]
      },
      listQuery: {
        page: 1,
        pageSize: 10,
        totalCount: 1,
        status: true
      },
      textMap: {
        update: '编辑互斥规则',
        create: '新建互斥规则'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      // 表格
      columns: [
        {
          prop: 'type',
          label: '互斥类型',
          render: (h, params) => {
            if (params.row.type === 1) {
              return h('el-tag', '小票互斥')
            } else {
              return h('el-tag', { props: { type: 'success' }}, '活动互斥')
            }
          }
        },
        {
          prop: 'scheduleAId',
          label: '档期主题A',
          formatter: (row, column) => {
            const { scheduleA } = row
            if (scheduleA) {
              return scheduleA.name
            }
          }
        }, {
          prop: 'scheduleBId',
          label: '档期主题B',
          formatter: (row, column) => {
            const { scheduleB } = row
            if (scheduleB) {
              return scheduleB.name
            }
          }
        }
      ],
      listLoading: false,
      list: [],
      listOptions: {
        stripe: true
      }
    }
  },
  created() {
    this.hdlList()
  },
  methods: {

    changeScheduleA(item) {
      this.dataForm.scheduleAId = item.id
    },
    changeScheduleB(item) {
      this.dataForm.scheduleBId = item.id
    },
    changeScheduleQuery(item) {
      this.listQuery.scheduleId = item.id
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container{
  .disabled {
    ::v-deep{
      .el-input{
        width: 140px;
      }
    }
  }
}
</style>
